import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

main() {
  runApp(MyApp());
}

final GoRouter _router = GoRouter(
  initialLocation: '/',
  routes: [
    ShellRoute(
      builder: (context, state, child) {
        return HomePage(child);
      },
      routes: [
        ShellRoute(
          builder: (context, state, child) {
            return Page1(child);
          },
          routes: [
            GoRoute(
              path: '/',
              builder: (context, state) {
                return Page2(); // 左侧页面
              },
            ),
            GoRoute(
              path: '/page3', // 新页面路径
              builder: (context, state) {
                return Page3(); // 新页面
              },
            )
          ],
        ),
      ],
    ),
  ],
);

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
    );
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      height: 200,
      color: Colors.yellowAccent,
    );
  }
}

class Page3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.green,
      child: Center(
        child: TextButton(
            onPressed: () {
              context.pop();
            },
            child: Text('return')),
      ),
    );
  }
}

class Page1 extends StatelessWidget {
  Widget child;

  Page1(this.child);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Container(
          width: 200,
          color: Colors.pinkAccent,
          child: Center(
            child: TextButton(
                onPressed: () {
                  print(1);
                  context.push('/page3');
                },
                child: Text('data')),
          ),
        ),
        Expanded(child: child)
      ],
    );
  }
}

class HomePage extends StatelessWidget {
  Widget child;

  HomePage(this.child);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(child: child),
          Container(
            height: 100,
            color: Colors.blue,
          )
        ],
      ),
    );
  }
}
